<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use \Cache;
use Carbon\Carbon;
/**
 * Created by PhpStorm.
 * User: 郭伟
 * Date: 2019-12-16
 * Time: 16:55
 */

class MiniController extends Controller
{

    private $app;

    public function __construct()
    {
        $this->app = Factory::miniProgram(config('wechat.mini_program.default'));
    }

    /**
     * 小程序解密的接口
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function decrypt(Request $request)
    {
        $request->validate([
            'encrypt_data' => 'required|string',
            'iv' => 'required|string',
            'code' => 'required|string',
        ]);
        $app = Factory::miniProgram(config('wechat.mini_program.default'));
        $session = $app->auth->session($request->code);
        if (!$session['openid']) {
            return $this->errorReturn('获取openid失败');
        }
        $sessionKey = $session['session_key'];
//        $sessionKey = Cache::get('session_key_' . openid());
        if (empty($sessionKey)) {

            return response('session_key was time out. Please refresh it then try it again.', 403);
        }
        try {
            $tool = $this->app->encryptor;
            $decryptedData = $tool->decryptData($sessionKey, $request->iv, $request->encrypt_data);

            return $this->successReturn($decryptedData);
        } catch (\Throwable $e) {
            return $this->errorReturn($e->getMessage());
        }
    }


    /**
     * 抖音小程序解密的接口
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function ttDecrypt(Request $request)
    {
        $request->validate([
            'encrypt_data' => 'required|string',
            'iv' => 'required|string',
            'code' => 'required|string',
        ]);
        $app = new \Qbhy\TtMicroApp\TtMicroApp([
            'access_key' => 'tt3ecd4870a1f7dbb0',
            'secret_key' => '4880224bb36e457637418715447bed6fa314b570',
            'debug' => true,
        ]);
        $session = $app->auth->session($request->code);

        if (!$session['openid']) {
            return $this->errorReturn('获取openid失败');
        }

        $sessionKey = $session['session_key'];
//        $sessionKey = Cache::get('session_key_' . openid());
        if (empty($sessionKey)) {

            return response('session_key was time out. Please refresh it then try it again.', 403);
        }
        try {

            $decryptedData = $app->decrypt->decrypt($request->encrypt_data, $sessionKey, $request->iv);

            return $this->successReturn($decryptedData);
        } catch (\Throwable $e) {
            return $this->errorReturn($e->getMessage());
        }
    }


}
